package com.ntv.presentation.spring.web.wishes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ntv.domain.IWishes;
import com.ntv.domain.Wishes;
import com.ntv.presentation.spring.web.news.NewsHomeController;
import com.ntv.service.wishes.IWishesAppService;

@Controller
@RequestMapping("/wishesshome.html")
public class WishesHomeController {

	private static final Logger logger = Logger
			.getLogger(NewsHomeController.class);

	@Autowired
	private IWishesAppService wishesAppService;

	/**
	 * @param wishesAppService
	 *            the wishesAppService to set
	 */
	public void setWishesAppService(IWishesAppService wishesAppService) {
		this.wishesAppService = wishesAppService;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView form(Model modelll) throws Exception {
		// ModelAndView mav = new ModelAndView();
		modelll.addAttribute(MA_WISHES, new Wishes());
		List<IWishes> wishesList = wishesAppService.getCurrentDayWishes();
		logger.debug(" List Size " + wishesList.size());
		Map model = new HashMap();
		model.put("wishesList", wishesList);
		ModelAndView mav = new ModelAndView("home.wishes", model);
		return mav;
	}
	
	
	@InitBinder
	public void initBinder(WebDataBinder webDataBinder){
		logger.info(" initBinder of wishes home controller ");
		webDataBinder.setAllowedFields(new String[] { "publishDateString" });
	}
	
	private static final String MA_WISHES = "wishes";
	
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView form(@ModelAttribute(MA_WISHES) Wishes wishes) throws Exception{
		
		List<IWishes> wishesList = this.wishesAppService.getWishesByDate(wishes.getPublishDateString());
		
		Map model = new HashMap();
		model.put("wishesList", wishesList);
		ModelAndView modelAndView = new ModelAndView("home.wishes", model);
		
		return modelAndView;
	}
	
}
